/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import com.ykht.proj.coupon.entity.CnBrand;
import com.ykht.proj.coupon.entity.FxConfPosterBrand;
import com.ykht.proj.coupon.service.FxConfPosterBrandService;
import com.ykht.proj.coupon.vo.FxConfPosterBrandVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 品牌海报配置表
 *
 * @author proj code generator
 * @date 2021-08-12 14:10:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxconfposterbrand" )
@Api(value = "fxconfposterbrand", tags = "品牌海报配置表管理")
@Slf4j
public class FxConfPosterBrandController {

    private final  FxConfPosterBrandService fxConfPosterBrandService;

    /**
     * 分页查询 品牌海报配置表
     * @param page 分页对象
     * @param fxConfPosterBrandVo 品牌海报配置表
     * @return
     */
    @ApiOperation(value = "分页查询 品牌海报配置表", notes = "分页查询 品牌海报配置表")
    @GetMapping("/page" )
    public R<IPage<FxConfPosterBrandVo>> getFxConfPosterBrandPage(Page page, FxConfPosterBrandVo fxConfPosterBrandVo) {
		log.info("->分页查询 品牌海报配置表 :{}", page, fxConfPosterBrandVo);
    	IPage<FxConfPosterBrandVo> iPage = fxConfPosterBrandService.getFxConfPosterBrandPage(page, fxConfPosterBrandVo);
        return R.ok(iPage);
    }


    /**
     * 通过id查询品牌海报配置表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fxConfPosterBrandService.getById(id));
    }

    /**
     * 新增品牌海报配置表
     * @param fxConfPosterBrand 品牌海报配置表
     * @return R
     */
    @ApiOperation(value = "新增品牌海报配置表", notes = "新增品牌海报配置表")
    @SysLog("新增品牌海报配置表" )
    @PostMapping
    public R save(@RequestBody FxConfPosterBrand fxConfPosterBrand) {
		log.info("新增品牌海报配置表:{}", fxConfPosterBrand);
		try {
			Long sjid = SecurityUtils.getSjId();
			FxConfPosterBrand posterBrand = fxConfPosterBrandService.getOne(Wrappers.lambdaQuery(FxConfPosterBrand.class)
				.eq(FxConfPosterBrand::getBrandId, sjid));
//		if (null != brand){
//			return R.failed("一个品牌只能有一张海报");
//		}
			CnBrand brand1 = new CnBrand().selectById(sjid);
			if (brand1!=null){
				fxConfPosterBrand.setCompanyId(brand1.getCompanyId());
			}
			if (posterBrand != null) {
				fxConfPosterBrand.setId(posterBrand.getId());
				fxConfPosterBrand.setUpdateTime(LocalDateTime.now());
				fxConfPosterBrand.setUpdateUser(SecurityUtils.getUserId());
			}else {
				fxConfPosterBrand.setCreateTime(LocalDateTime.now());
				fxConfPosterBrand.setCreateUser(SecurityUtils.getUserId());
			}
			fxConfPosterBrand.setBrandId(sjid);
			//海报活动：开启或关闭需要审核，海报名称+和海报变更时才需要审核
			int count = fxConfPosterBrandService.count(Wrappers.lambdaQuery(FxConfPosterBrand.class)
					.eq(FxConfPosterBrand::getBrandId, sjid)
					.eq(FxConfPosterBrand::getName,fxConfPosterBrand.getName())
					.eq(FxConfPosterBrand::getSwitchs,fxConfPosterBrand.getSwitchs())
					.eq(FxConfPosterBrand::getUrl,fxConfPosterBrand.getUrl()));
			if ((count == 0 && posterBrand != null) || posterBrand == null){//第一创建或者有了改动，变为待审核
				fxConfPosterBrand.setStatus(2);//待审核
				fxConfPosterBrand.setPushTime(LocalDateTime.now());
				//再次提交审核，需要把审核的内容和时间值为空
				fxConfPosterBrand.setExamineTime(null);
				fxConfPosterBrand.setExamineReason(null);
			}
			fxConfPosterBrandService.saveOrUpdate(fxConfPosterBrand);
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed("海报保存失败");
		}
	}
    @ApiOperation(value = "品牌的海报详情")
    @GetMapping("info")
    public R info() {
		Long sjid = SecurityUtils.getSjId();
		log.info("品牌的海报详情:{}",sjid);
		FxConfPosterBrand one = fxConfPosterBrandService.getOne(Wrappers.lambdaQuery(FxConfPosterBrand.class).eq(FxConfPosterBrand::getBrandId, sjid));
		return R.ok(one);
    }

	@ApiOperation(value = "校验 品牌海报配置表名称")
	@PostMapping("/checkPosterName")
	public R checkPosterName(@RequestBody FxConfPosterBrand query) {
		log.info("->校验 品牌海报配置表名称{}", query);
		FxConfPosterBrand one = fxConfPosterBrandService.getOne(Wrappers.lambdaQuery(FxConfPosterBrand.class)
				.eq(!StringUtils.isEmpty(query.getName()), FxConfPosterBrand::getName, query.getName()));
		return R.ok(one == null ? true : one.getId().equals(query.getId()) ? true : false);
	}

    /**
     * 修改品牌海报配置表
     * @param fxConfPosterBrand 品牌海报配置表
     * @return R
     */
    @ApiOperation(value = "修改品牌海报配置表", notes = "修改品牌海报配置表")
    @SysLog("修改品牌海报配置表" )
    @PutMapping
    public R updateById(@RequestBody FxConfPosterBrand fxConfPosterBrand) {
		fxConfPosterBrand.setUpdateUser(SecurityUtils.getUserId());
		fxConfPosterBrand.setUpdateTime(LocalDateTime.now());
		fxConfPosterBrand.setStatus(1);
        return R.ok(fxConfPosterBrandService.updateById(fxConfPosterBrand));
    }

    /**
     * 通过id删除品牌海报配置表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除品牌海报配置表", notes = "通过id删除品牌海报配置表")
    @SysLog("通过id删除品牌海报配置表" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fxConfPosterBrandService.removeById(id));
    }


	@ApiOperation(value = "品牌海报 审核")
	@PostMapping("/updateStatus")
	public R updateStatus(@RequestBody FxConfPosterBrand fxConfPosterBrand) {
		log.info("->品牌海报 审核{}", fxConfPosterBrand);
		if (fxConfPosterBrand.getSwitchs()!=Integer.valueOf(1)){//0通过1未通过2未审核
			fxConfPosterBrand.setExamineReason(null);
		}
		fxConfPosterBrand.setUpdateUser(SecurityUtils.getUserId());
		fxConfPosterBrand.setUpdateTime(LocalDateTime.now());
		fxConfPosterBrand.setExamineTime(LocalDateTime.now());
		fxConfPosterBrandService.updateById(fxConfPosterBrand);
		return R.ok();
	}

	@ApiOperation(value = "品牌海报 上下架")
	@PostMapping("/updateSwitchs")
	public R updateSwitchs(@RequestBody FxConfPosterBrand fxConfPosterBrand) {
		log.info("->品牌海报 上下架{}", fxConfPosterBrand);
		//关闭了之后需要重新审核
		if (SecurityUtils.getUser().getUserType()== UserTypeEnum.admin.getValue() && fxConfPosterBrand.getSwitchs()==Integer.valueOf(1)){
			fxConfPosterBrand.setStatus(1);
		}
		fxConfPosterBrand.setUpdateUser(SecurityUtils.getUserId());
		fxConfPosterBrand.setUpdateTime(LocalDateTime.now());
		fxConfPosterBrandService.updateById(fxConfPosterBrand);
		return R.ok();
	}


}
